package short_semester.questionnaire_planet.service.analyse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import short_semester.questionnaire_planet.dao.*;
import short_semester.questionnaire_planet.pojo.*;
import short_semester.questionnaire_planet.response.fillInResponse.FillInResponse;
import short_semester.questionnaire_planet.response.fillResponse.OptionRecordResponse;
import short_semester.questionnaire_planet.response.fillResponse.QuestionRecordResponse;

import java.util.*;

@Service
public class MultipleChoiceQuestionRecordService {
    @Autowired
    MultipleChoiceQuestionRecordDao multipleChoiceQuestionRecordDao;

    @Autowired
    QuestionnaireDao questionnaireDao;

    @Autowired
    MultiChoiceQuestionDao multiChoiceQuestionDao;

    @Autowired
    MultiChoiceOptionDao multiChoiceOptionDao;
    

    public void addRecord(
            Integer questionId,
            QuestionnaireRecord questionnaireRecord,
            List<Integer> options) {
        Optional<MultiChoiceQuestion> multiChoiceQuestion = multiChoiceQuestionDao.findById(questionId);
        if (!multiChoiceQuestion.isPresent())
            return;
        MultiChoiceQuestion question = multiChoiceQuestion.get();
        Optional<MultiChoiceQuestionRecord> ret = multipleChoiceQuestionRecordDao
                .findByQuestionnaireRecordAndMultiChoiceQuestion(questionnaireRecord, question);
        MultiChoiceQuestionRecord record = null;
        if (ret.isPresent()) {
            record = ret.get();
            record.setAnswer(options);
        }
        else {
            record = new MultiChoiceQuestionRecord();
            record.setAnswer(options);
            record.setMultiChoiceQuestion(question);
            record.setQuestionnaireRecord(questionnaireRecord);
        }
        multipleChoiceQuestionRecordDao.save(record);
    }

    public void submitRecord(QuestionnaireRecord questionnaireRecord){
        for (MultiChoiceQuestionRecord questionRecord : multipleChoiceQuestionRecordDao.findAllByQuestionnaireRecord(questionnaireRecord)) {

            //有答案，不为空
            if(!questionRecord.getAnswer().isEmpty()){
                multiChoiceQuestionDao.submitQuestion(questionRecord.getMultiChoiceQuestion().getId());
                for (Integer optionId : questionRecord.getAnswer()) {
                    multiChoiceOptionDao.submitOption(optionId);
                }
            }
            
        }
    }

    //获取题目记录信息
    public QuestionRecordResponse getRecord(QuestionnaireRecord record, Integer questionId){
        //题目基本信息
        MultiChoiceQuestion question = multiChoiceQuestionDao.getById(questionId);
        MultiChoiceQuestionRecord questionRecord = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question).get();
        QuestionRecordResponse questionRecordResponse = new QuestionRecordResponse(question,questionRecord);

        //选项信息
        for (Integer optionId : questionRecord.getOptionSequence()) {
            MultiChoiceOption option = multiChoiceOptionDao.getById(optionId);
            OptionRecordResponse optionRecordResponse = new OptionRecordResponse(option);
            questionRecordResponse.getOptions().add(optionRecordResponse);
        }
        return  questionRecordResponse;
    }
    
    //计算分数
    public int compute(QuestionnaireRecord record,Integer questionId){
        int grade=0;
        MultiChoiceQuestion question = multiChoiceQuestionDao.getById(questionId);
        MultiChoiceQuestionRecord questionRecord = multipleChoiceQuestionRecordDao.findByQuestionnaireRecordAndMultiChoiceQuestion(record, question).get();
        Set<Integer> answer = new HashSet<>(questionRecord.getAnswer());
        for (MultiChoiceOption option : multiChoiceOptionDao.findByMultiChoiceQuestion(question)) {
            if(option.getIsCorrect()){
                if(answer.contains(option.getId())){
                    answer.remove(option.getId());
                }else {
                    break;
                }
            }
        }
        if(answer.isEmpty())
            grade+=question.getGrade();
        
        return grade;
    }
}
